'use strict';

const Controller = require('egg').Controller;

class ListController extends Controller {
    async index() {
        const { ctx } = this;
        const { pageIndex, pageSize, value } = ctx.query
        const data = await this.app.mysql.query(`SELECT * FROM shop WHERE title LIKE '%${value}%' LIMIT ${pageIndex},${pageSize}`)
        const total = await this.app.mysql.query(`SELECT * FROM shop WHERE title LIKE '%${value}%'`)
        if (data.length > 0) {
            ctx.body = {
                code: 200,
                data,
                total: total.length
            }
        } else {
            ctx.body = {
                code: 400,
                mes: '查询不到数据'
            }
        }
    }
    async delList() {
        const { ctx } = this;
        const { id } = ctx.query
        const data = await this.app.mysql.delete('shop', { id });
        if(data.affectedRows){
            ctx.body={
                code:200,
                mes:"删除成功"
            }
        }else{
            ctx.body={
                code:400,
                mes:"删除失败"
            }
        }
    }
}

module.exports = ListController;
